<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Auth extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->model('Query_builder_model', 'qb');
        $param = array(
            'controller' => $this->controller
        );
        $this->load->library('User', '', 'u');
        
        $this->load->library('User', '', 'u');
        $this->load->library('Template', $param, 'tpl');
    }

    private $controller = 'banners';

    public function index($action='list', $id=0) {
        
        if($this->u->loged){
            header('Location: '.BASE_URL.'users/' );
        }
        
        $this->tpl->set('css',array('auth'));
        $this->tpl->set('main','default/login');
        $this->tpl->set('jquery_ui',true);
        $this->tpl->set('js_exeption',array('object'));
        echo $this->tpl->render();
    }
    
    function signup(){
        $json = array('success'=>'error','msg'=>'Code miss match!','refresh_captcha'=>'yes');
        
        $post_captcha = $this->input->post('captcha');
        
        $captcha = $this->session->userdata('captcha');
        
        $json['msg'] = 'Code miss match';
        
        if($captcha==$post_captcha){
            
            $form_attributes = $GLOBALS['table']['users'];
            $save = array();
            
            foreach ($form_attributes as $i => $v){
                $save[$i] = $this->input->post($i);
            }
            
            if(!$this->qb->record_exist(array('from'=>'users','field'=>'mail', 'key'=>$save['mail']))){
                $json['msg'] = 'Saving Failed!';
                if($this->db->insert('users',$save)){
                    $json['msg'] = 'You can login using your new account info!';
                    $json['success'] = 'success';
                    unset ($json['refresh_captcha']);
                }
                
            }else{
                $json['msg'] = 'Email already exist!';
            }
            
        }
        
        echo json_encode($json);
    }
    
    function logout(){
        $this->session->unset_userdata('user');
        $this->index();
    }
    
    function login(){
        
        $json = array();
        $json['msg'] = 'User not found!';
        $json['success'] = 'error';
        
        $pass = $this->input->post('password');
        $email = $this->input->post('username');
        
        
        $res = $this->qb->select(array('from'=>'users','where'=>array('mail'=>$email),'limit'=>'1'));
        
        if($res){
            $res = $res->row_array();
            if($res['pass']==$pass){
                $this->session->set_userdata(array('user'=>$res));
                $json['msg'] = 'Login successfull!';
                $json['success'] = 'success';
                $json['redirect'] = BASE_URL.'users/';
            }else{
                $json['msg'] = 'Password miss match!';
            }
        }
        
        echo json_encode($json);
    }
    
    function captcha($id=1){
        
        header("Content-Type: image/png");
        $num = rand(1, 1000);
        $string = substr(md5($num), 5, 6);
        $this->session->set_userdata(array('captcha'=>$string));
        $im = @imagecreate(80, 30)
            or die("Cannot Initialize new GD image stream");
        $background_color = imagecolorallocate($im, 0, 0, 0);
        $text_color = imagecolorallocate($im, 233, 14, 91);
        imagestring($im, 3, 19, 7,  $string, $text_color);
        imagepng($im);
        imagedestroy($im);
        
    }
    
    
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */